<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      :border="false"
      fit
      style="width: 100%;"
    >
      <el-table-column label="ID" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="消费">
        <template slot-scope="{row}">
          <span style="color: red">￥{{ +row.balance }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型">
        <template slot-scope="{row}">
          <span>{{ row.text }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="{row}">
          <span>{{ row.status | statusText }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单">
        <template slot-scope="{row}" v-if="+row.order_id > 0">
          <el-link @click="$router.push('/order?id=' + row.order_id)">{{ row.order_id }} 点击查看</el-link>
        </template>
      </el-table-column>
      <el-table-column label="时间" width="200">
        <template slot-scope="{row:{create_time}}">
          <i class="el-icon-time" />
          <span style="margin-left: 5px">{{ create_time }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="listQuery.total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.pageSize"
      :page-sizes="[5, 10, 20, 50, 100]"
      @pagination="refresh"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'
import api from '@/api'

const initQuery = {
  page: 1,
  pageSize: 10,
  total: 0,
  sort: '+id'
}

export default {
  name: 'MemberConsume',
  directives: { waves },
  components: { Pagination },
  data() {
    return {
      listLoading: false,
      list: [],
      listQuery: {}
    }
  },
  filters: {
    statusText(v) {
      return +v === 1 ? '使用' : '退还'
    }
  },
  created() {
    this.parseQuery()
  },
  mounted() {
    this.getList()
  },
  methods: {
    parseQuery() {
      const query = Object.assign({}, this.$route.query)
      this.listQuery = Object.assign({}, { ...initQuery })
      if (Object.keys(query).length > 0) {
        // 注意这里的类型转换
        query.page && (query.page = +query.page)
        query.pageSize && (query.pageSize = +query.pageSize)
        query.total && (query.total = +query.total)
      }
    },
    refresh() {
      this.$router.push({
        path: '/member/consume/' + this.$route.params.user_id,
        query: this.listQuery
      })
    },
    getList() {
      this.listLoading = true
      api.member.getConsume({
        user_id: this.$route.params.user_id,
        ...this.listQuery
      }).then(response => {
        const { data } = response
        this.list = data.list
        this.listQuery.total = +data.totalCount
      }).finally(() => {
        this.listLoading = false
      })
    }
  },
  beforeRouteUpdate(to, from, next) {
    if (to.path === from.path) {
      const newQuery = Object.assign({}, to.query)
      const oldQuery = Object.assign({}, from.query)
      if (JSON.stringify(newQuery) !== JSON.stringify(oldQuery)) {
        this.getList()
      }
    }
    next()
  }
}
</script>
